package com.wooophone.file.action;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.aspectj.util.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;

import com.wooophone.common.util.ID;
import com.wooophone.common.web.action.BaseAction;
import com.wooophone.file.biz.FileEntityBiz;
import com.wooophone.file.entity.FileEntity;

public class FileUploadAction extends BaseAction {

	private static final Logger log = Logger.getLogger(FileUploadAction.class);

	private static final long serialVersionUID = 1L;

	@Autowired
	private FileEntityBiz fileEntityBiz;

	public void setFileEntityBiz(FileEntityBiz fileEntityBiz) {
		this.fileEntityBiz = fileEntityBiz;
	}

	// 目录文件夹
	private String folder;
	// 接收到的文件
	private File uploadify;
	// 接收到的文件名
	private String uploadifyFileName;
	// 类型
	private String uploadifyContentType;
	// 文件限制类型
	private String fileext;

	@SuppressWarnings("finally")
	public String uploadifyUpload() {
		
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			if (uploadify != null) {

				log.debug("uploadifyContentType:" + uploadifyContentType);
				log.debug("fileext:" + fileext);
				log.debug("folder:" + folder);
				log.debug("uploadify:" + uploadify);
				log.debug("uploadifyFileName:" + uploadifyFileName);

				String simplePath = "/files" + folder + "/";

				FileEntity fileEntity = new FileEntity();
				fileEntity.setId(ID.uuid());
				fileEntity.setName(uploadifyFileName);
				fileEntity.setSize(uploadify.length());

				String ext;
				if (uploadifyFileName != null
						&& uploadifyFileName.lastIndexOf(".") != -1) {
					ext = uploadifyFileName.substring(uploadifyFileName
							.lastIndexOf("."));
				} else {
					ext = null;
				}
				fileEntity.setExt(ext);
				fileEntity.setPath(simplePath + fileEntity.getId()
						+ fileEntity.getExt());
				fileEntity.setUploadIp(getIpAddr());
				fileEntity.setUploader(fileEntity.getUploadIp());

				fileEntityBiz.save(fileEntity);

				if (fileEntity.getId() != null) {
					File file = new File(ServletActionContext
							.getServletContext().getRealPath(
									fileEntity.getPath()));

					FileUtil.copyFile(uploadify, file);
					
					resultMap.put("status", "success");
					resultMap.put("id", fileEntity.getId());
				} else {
					resultMap.put("status", "error");
				}

				resultMap.put("name", fileEntity.getName());
				resultMap.put("path", fileEntity.getPath());

			} else {
				resultMap.put("name", null);
				resultMap.put("path", null);
				resultMap.put("status", "error");
				resultMap.put("id", null);
			}
		} catch (Exception e) {
			resultMap.put("name", uploadifyFileName);
			resultMap.put("path", null);
			resultMap.put("status", "error");
			resultMap.put("id", null);
			e.printStackTrace();
		}finally{
			return ajax(resultMap);
		}
	}

	public void setUploadify(File uploadify) {
		this.uploadify = uploadify;
	}

	public String getUploadifyFileName() {
		return uploadifyFileName;
	}

	public void setUploadifyFileName(String uploadifyFileName) {
		this.uploadifyFileName = uploadifyFileName;
	}

	public String getFolder() {
		return folder;
	}

	public void setFolder(String folder) {
		this.folder = folder;
	}

	public String getUploadifyContentType() {
		return uploadifyContentType;
	}

	public void setUploadifyContentType(String uploadifyContentType) {
		this.uploadifyContentType = uploadifyContentType;
	}

	public String getFileext() {
		return fileext;
	}

	public void setFileext(String fileext) {
		this.fileext = fileext;
	}

	public File getUploadify() {
		return uploadify;
	}
}
